package com.evendai.sipdemo.sip.listener

import android.view.MotionEvent
import android.view.View
import android.view.WindowManager

class FloatViewOnTouchListener(
    private val params: WindowManager.LayoutParams,
    private val manager: WindowManager
) : View.OnTouchListener {

    private var downX = 0
    private var downY = 0
    private var moveX = 0
    private var moveY = 0
    private var hasMove = false

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                hasMove = false
                downX = event.rawX.toInt()
                downY = event.rawY.toInt()
            }
            MotionEvent.ACTION_MOVE -> {
                hasMove = true
                moveX = event.rawX.toInt()
                moveY = event.rawY.toInt()
                params.x += downX - moveX
                params.y += downY - moveY
                downX = moveX
                downY = moveY
                manager.updateViewLayout(v, params)
            }
            MotionEvent.ACTION_UP -> {
                if (!hasMove) {
                    v.performClick()
                }
            }
        }
        return true
    }

}
